/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.fspike.gui.dialog;

import java.awt.Frame;
import java.util.List;
import java.util.Map;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import uk.ac.ebi.kraken.interfaces.uniprot.Keyword;
import uk.ac.ebi.kraken.interfaces.uniprot.UniProtEntry;

/**
 *
 * @author Kenneth
 */
public class DialogConfirmContaminants extends javax.swing.JDialog {

    private Map<String, String> uniprotSpikeSequencesByName;
    private Map<String, String> contaminationMap;
    private Frame parentFrame;

    /**
     * Creates new form DialogConfirmContaminants
     */
    public DialogConfirmContaminants(Map<String, String> contaminationMap, Map<String, String> uniprotSpikeSequencesByName, java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parentFrame = parent;
        this.setLocationRelativeTo(parent);
        this.contaminationMap = contaminationMap;
        this.uniprotSpikeSequencesByName = uniprotSpikeSequencesByName;
        //fill the left list with the uniprot names...
        DefaultListModel model = new DefaultListModel();
        for (String anEntry : uniprotSpikeSequencesByName.keySet()) {
            StringBuilder contaminantDescription = new StringBuilder(anEntry);
            model.addElement(contaminantDescription.toString());
        }
        jListPossible.setModel(model);
        jListPossible.repaint();
        jListPossible.revalidate();
        jListConfirmed.setModel(new DefaultListModel());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListConfirmed = new javax.swing.JList();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListConfirmed = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListPossible = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jListPossible = new javax.swing.JList();
        addToConfirmedButton = new javax.swing.JButton();
        removeFromConfirmedButton = new javax.swing.JButton();
        confirmationButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));

        jScrollPane1.setViewportView(jListConfirmed);

        jScrollPane1.setViewportView(jListConfirmed);

        jScrollPane2.setViewportView(jListPossible);

        jScrollPane2.setViewportView(jListPossible);

        addToConfirmedButton.setText(">");
        addToConfirmedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addToConfirmedButtonActionPerformed(evt);
            }
        });

        removeFromConfirmedButton.setText("<");
        removeFromConfirmedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeFromConfirmedButtonActionPerformed(evt);
            }
        });

        confirmationButton.setText("Add");
        confirmationButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                confirmationButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 280, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(addToConfirmedButton)
                            .addComponent(removeFromConfirmedButton))
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(cancelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(confirmationButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane2});

        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(122, 122, 122)
                        .addComponent(addToConfirmedButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(removeFromConfirmedButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE)
                    .addComponent(jScrollPane1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(confirmationButton)
                    .addComponent(cancelButton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addToConfirmedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addToConfirmedButtonActionPerformed
        List selectedValuesList = jListPossible.getSelectedValuesList();
        DefaultListModel model = (DefaultListModel) jListConfirmed.getModel();
        for (Object anEntry : selectedValuesList) {
            model.addElement((String) anEntry);
        }
    }//GEN-LAST:event_addToConfirmedButtonActionPerformed

    private void removeFromConfirmedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeFromConfirmedButtonActionPerformed
        List selectedValuesList = jListConfirmed.getSelectedValuesList();
        DefaultListModel model = (DefaultListModel) jListConfirmed.getModel();
        for (Object anEntry : selectedValuesList) {
            model.removeElement((String) anEntry);
        }
    }//GEN-LAST:event_removeFromConfirmedButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void confirmationButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_confirmationButtonActionPerformed
        for (int i = 0; i < jListConfirmed.getModel().getSize(); i++) {
            //find the original uniprot entry
            String uniprotHeader = (String) jListConfirmed.getModel().getElementAt(i);
            contaminationMap.put(uniprotHeader, uniprotSpikeSequencesByName.get(uniprotHeader));
        }
        this.dispose();
    }//GEN-LAST:event_confirmationButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addToConfirmedButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JButton confirmationButton;
    private javax.swing.JList jListConfirmed;
    private javax.swing.JList jListPossible;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton removeFromConfirmedButton;
    // End of variables declaration//GEN-END:variables
}
